package com.chain.BrotherExchange.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.Payment;

import java.util.List;

/**
 * 支付方式对话框
 */
public class PaymentDialog extends BottomSheetDialog {

    public interface OnClickListener {
        void onClick(Payment payment);
    }

    public static void show(Context context, List<Payment> list, OnClickListener onClickListener) {
        new PaymentDialog(context, list, onClickListener).show();
    }

    private PaymentDialog(@NonNull Context context, List<Payment> list, OnClickListener onClickListener) {
        super(context);

        // root
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundColor(Color.WHITE);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        setContentView(linearLayout);

        // 支付方式标题
        createTitleView(linearLayout);

        // 具体支付方式
        for (Payment payment : list) {
            TextView text = (TextView) LayoutInflater.from(context).inflate(
                    android.R.layout.simple_list_item_1, linearLayout, false);
            text.setText(payment.getPaymentName());
            linearLayout.addView(text);

            text.setOnClickListener(v -> {
                onClickListener.onClick(payment);
                dismiss();
            });
        }
    }

    private void createTitleView(LinearLayout layout) {
        Context context = layout.getContext();
        TextView title = (TextView) LayoutInflater.from(context).inflate(
                android.R.layout.simple_list_item_1, layout, false);
        title.setText(R.string.text_please_select_payment_method);
        title.setGravity(Gravity.CENTER);

        View divider = new View(context);
        divider.setBackgroundColor(context.getResources().getColor(R.color.colorDivider));
        divider.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));

        layout.addView(title);
        layout.addView(divider);
    }
}
